package com.xiaoze.pool.utils;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class HtmlUtils {

    private static final Logger logger = LoggerFactory.getLogger(HtmlUtils.class);

    private static final String[][] HEADERS = new String[][]{
            {"Connection", "keep-alive"},
            {"Cache-Control", "max-age=0"},
            {"Upgrade-Insecure-Requests", "1"},
            {"User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko)"},
            {"Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"},
            {"Accept-Encoding", "gzip, deflate, sdch"},
            {"Accept-Language", "zh-CN,zh;q=0.8"},
    };

    public static String getHtml(String pageUrl) {
        String html = "";
        String url = pageUrl;
        logger.info("fetching page: " + url);
        try {
            Connection connection = Jsoup.connect(url);
            for (String[] head : HEADERS) {
                connection.header(head[0], head[1]);
            }
            connection.timeout(4000).followRedirects(true);
            //执行
            html = connection.execute().parse().html();
        } catch (IOException e) {
            logger.info("fetch page error: " + e.getMessage());
        }

        return html;
    }

    public static boolean checkTime(String ip, int port){

        try {
            Jsoup.connect("http://www.baidu.com")
                    .timeout(1000)
                    .proxy(ip,port)
                    .get();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
